package com.learnjava.learnjava1.controller;

import com.learnjava.learnjava1.dto.DepartmentDTO;
import com.learnjava.learnjava1.dto.DepartmentShortDTO;
import com.learnjava.learnjava1.service.DepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor

@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @Operation(summary = "Создание департамента")
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public DepartmentDTO create(@RequestBody DepartmentDTO dto) {
        return departmentService.create(dto);
    }

    @Operation(summary = "Получить департамент по id")
    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public DepartmentDTO getDepartamentById(@PathVariable Long id) {
        return departmentService.getDepartmentById(id);
    }

    @Operation(summary = "Получить список всех департаментов")
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public Page<DepartmentDTO> readAll(@PageableDefault(value = 10, page = 0) Pageable pageable) {
        return departmentService.readAll(pageable);
    }

    @Operation(summary = "Фильтрация")
    @GetMapping(path = "filter")
    @ResponseStatus(HttpStatus.OK)
    public Page<DepartmentDTO> filterAll(@RequestParam(name = "title", required = false) String title,
                                         @RequestParam(name = "active", required = false) Boolean active,
                                         @RequestParam(name = "id", required = false) Long id,
                                         @RequestParam(name = "companyId", required = false) Long companyId,
                                         @PageableDefault(value = 10, page = 0) Pageable pageable) {
        return departmentService.filterAll(title, active, id, companyId, pageable);
    }

    @Operation(summary = "Обновление данных по департаменту")
    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public DepartmentShortDTO update(@RequestBody DepartmentShortDTO departmentDTO, @PathVariable Long id) {
        return departmentService.update(departmentDTO, id);
    }

    @Operation(summary = "Удалить компанию")
    @DeleteMapping("/{id}")
    public HttpStatus delete(@PathVariable Long id) {
        departmentService.delete(id);
        return HttpStatus.OK;
    }

}
